package coop.bancocredicoop.proyectos.gd.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.classic.Session;

import coop.bancocredicoop.proyectos.gd.Pager;
import coop.bancocredicoop.proyectos.gd.ServletUtils;
import coop.bancocredicoop.proyectos.gd.query.DataService;
import coop.bancocredicoop.proyectos.gd.reports.CountByReport;
import coop.bancocredicoop.proyectos.gd.reports.QueryResult;

public class CantidadDocumentos extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final String cantidadDocumentosJsp = "/jsp/cantidadDocumentos/index.jsp";
	private static final Log log = LogFactory.getLog(CantidadDocumentos.class);
	
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int limit = 50;
		int offset = ServletUtils.getParameterAsInteger(request, "offset");
		Date dateFrom = ServletUtils.getParameterAsDate(request, "dateFrom", null);
		Date dateTo = ServletUtils.getParameterAsDate(request, "dateTo", null);
		String reportType = ServletUtils.getParameterAsString(request, "reportType", null);
		String cajaId = ServletUtils.getParameterAsString(request, "cajaId", null);
		Long idPersona = ServletUtils.getParameterAsLong(request, "customerId", null);
		
		if (reportType != null) {
			DataService dataService = new DataService();
			Session session = dataService.getSession();
			QueryResult countByResult = null;
			CountByReport countByReport = new CountByReport();
			
			try {
				if (reportType.equals("byCustomer")) {
					countByResult = countByReport.countByCustomer(session, idPersona, cajaId, dateFrom, dateTo, offset, limit);
				} else if (reportType.equals("byBox")) {
					countByResult = countByReport.countByBox(session, idPersona, cajaId, dateFrom, dateTo, offset, limit);
				} else if (reportType.equals("byDate")) {
					countByResult = countByReport.countByDate(session, idPersona, cajaId, dateFrom, dateTo, offset, limit);
				} else {
					throw new Exception(String.format("Report Type not supported: %s", reportType));
				}
				
				Pager pager = new Pager(offset, limit, countByResult.getTotalItems());
				
				request.setAttribute("dateFrom", ServletUtils.getMapFromDate(dateFrom));
				request.setAttribute("dateTo", ServletUtils.getMapFromDate(dateTo));
				request.setAttribute("reportType", reportType);
				request.setAttribute("items", countByResult.getItems());			
				request.setAttribute("pager", pager);
				
			} catch (Exception e) {
				log.error("Error in CountByReport.countByCustomer report", e);
			} finally {
				dataService.closeSession(session);
			}

		}
		
		getServletContext().getRequestDispatcher(cantidadDocumentosJsp).forward(request, response);
		
    }
	
}
